// Step 7 (Final): Change the app's theme

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';

import 'package:flutter_app/demos/WidgetLayoutDemo.dart';
import 'package:flutter_app/demos/SignatureDemo.dart';
import 'package:flutter_app/demos/FadeAnimationDemo.dart';
import 'package:flutter_app/demos/ToggleWidgetDemo.dart';
import 'package:flutter_app/demos/UpdateTextDemo.dart';
import 'package:flutter_app/demos/LikeListDemo.dart';
import 'package:flutter_app/demos/AsyncDemo.dart';
import 'package:flutter_app/demos/ProgressIndicatorDemo.dart';
import 'package:flutter_app/demos/AssetsDemo.dart';
import 'package:flutter_app/demos/InputDemo.dart';
import 'package:flutter_app/demos/ShoppingListDemo.dart';
import 'package:flutter_app/demos/ThemeDemo.dart';
import 'package:flutter_app/demos/ImageDemo.dart';
import 'package:flutter_app/demos/ListDemo.dart';
import 'package:flutter_app/demos/RouteDemo.dart';
import 'package:flutter_app/demos/FutureBuilderDemo.dart';
import 'package:flutter_app/demos/WebSocketDemo.dart';
import 'package:flutter_app/demos/ProductIntroductionDemo.dart';
import 'package:flutter_app/demos/interactive/InteractiveDemo.dart';
import 'package:flutter_app/demos/DartSyntax.dart';

void main() {
  // 设置开启UI调试
//  debugPaintSizeEnabled = true;
  runApp(ListTestSampleApp());
}

class ListTestSampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    runTest();
    return MaterialApp(
      title: "Home",
      theme: ThemeData(primaryColor: Colors.blue),
      home: ListTestSamplePage(),
      routes: {
        "/widgetlayout": (BuildContext context) => WidgetLayoutDemo(),
        "/signature": (BuildContext context) => SignaturePainterContainer(),
        "/fadeanimation": (BuildContext context) => FadeAnimationDemo(),
        "/togglewidget": (BuildContext context) => ToggleWidgetDemo(),
        "/updatetext": (BuildContext context) => UpdateTextDemo(),
        "/likelist": (BuildContext context) => LikeListDemo(),
        "/async": (BuildContext context) => AsyncDemo(),
        "/progress": (BuildContext context) => ProgressIndicatorDemo(),
        "/assets": (BuildContext context) => AssetsDemo(),
        "/input": (BuildContext context) => InputDemo(),
        "/shoppinglist": (BuildContext context) => ShoppingListDemo(),
        "/theme": (BuildContext context) => ThemeDemo(
              title: "ThemeDemo",
            ),
        "/image": (BuildContext context) => ImageDemo(),
        "/list": (BuildContext context) => ListDemo(),
        "/route": (BuildContext context) => RouteDemo(),
        "/futurebuilder": (BuildContext context) => FutureBuilderDemo(),
        "/websocket": (BuildContext context) => WebSocketDemo(),
        "/productintroduction": (BuildContext context) => ProductIntroductionDemo(),
        "/interactive": (BuildContext context) => InteractiveDemo(),

      },
    );
  }
}

class ListTestSamplePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ListTestState();
  }
}

class ListTestState extends State<ListTestSamplePage> {
  final _datas = <Map<String, Object>>[];

  final _biggerFont = const TextStyle(
    fontSize: 18.0,
    color: Colors.deepPurple,
  );

  @override
  void initState() {
    super.initState();

    _datas.add({"title": "WidgetLayoutDemo", "router": "/widgetlayout"});
    _datas.add({"title": "SignatureDemo", "router": "/signature"});
    _datas.add({"title": "FadeAnimationDemo", "router": "/fadeanimation"});
    _datas.add({"title": "ToggleWidgetDemo", "router": "/togglewidget"});
    _datas.add({"title": "UpdateTextDemo", "router": "/updatetext"});
    _datas.add({"title": "LikeListDemo", "router": "/likelist"});
    _datas.add({"title": "AsyncDemo", "router": "/async"});
    _datas.add({"title": "ProgressIndicatorDemo", "router": "/progress"});
    _datas.add({"title": "AssetsDemo", "router": "/assets"});
    _datas.add({"title": "InputDemo", "router": "/input"});
    _datas.add({"title": "ShoppingListDemo", "router": "/shoppinglist"});
    _datas.add({"title": "ThemeDemo", "router": "/theme"});
    _datas.add({"title": "ImageDemo", "router": "/image"});
    _datas.add({"title": "ListDemo", "router": "/list"});
    _datas.add({"title": "RouteDemo", "router": "/route"});
    _datas.add({"title": "FutureBuilderDemo", "router": "/futurebuilder"});
    _datas.add({"title": "WebSocketDemo", "router": "/websocket"});
    _datas.add({"title": "ProductionIntroductionDemo", "router": "/productintroduction"});
    _datas.add({"title": "InteractiveDemo", "router": "/interactive"});

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Home",
        ),
      ),
      body: _buildList(),
    );
  }

  Widget _buildList() {
    return ListView.builder(
        padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
        itemCount: _datas.length,
        itemBuilder: (context, index) {
          if (index < _datas.length) {
            Map data = _datas[index];
            return _buildItem(data);
          }
          return null;
        });
  }

  Widget _buildItem(Map data) {
    String title = data["title"];
    return new Container(
      child: ListTile(
        title: Text(
          title,
          style: _biggerFont,
        ),
        onTap: () {
          _onItemTap(data);
        },
      ),
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
        color: Colors.black12,
        width: 1,
      ))),
    );
  }

  void _onItemTap(Map data) {
    Navigator.of(context).pushNamed(data["router"]);
  }
}
